package utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;

public class WishModule<T>
{
	private List<WriteHandler> writeHandlers = new ArrayList<>();// 耦合点

	private String sheetName = "";

	private List<WishColumn<?>> tableHeaders = new ArrayList<>();

	private List<Supplier<List<?>>> datasSuppliers = new ArrayList<>();
	
	private Integer number=null;

	public WishModule()
	{
		super();
	}

	public WishModule(String sheetName)
	{
		super();
		this.sheetName = sheetName;
	}
	
	public WishModule(String sheetName,int number)
	{
		super();
		this.sheetName = sheetName;
		this.number = number;
	}

	public WishModule(List<WishColumn<?>> tableHeaders, Supplier<List<?>> datasSupplier)
	{
		super();
		this.tableHeaders = tableHeaders;
		this.datasSuppliers.add(datasSupplier);
	}

	public WishModule(String sheetName, List<WishColumn<?>> tableHeaders, Supplier<List<?>> datasSupplier)
	{
		super();
		this.sheetName = sheetName;
		this.tableHeaders = tableHeaders;
		this.datasSuppliers.add(datasSupplier);
	}

	public void setMerges(List<int[]> merges)
	{
		List<WriteHandler> list = new ArrayList<>();
		for (int[] items : merges)
		{
			list.add(new OnceAbsoluteMergeStrategy(items[0], items[1], items[2], items[3]));
		}
		writeHandlers.addAll(list);
	}

	public List<WriteHandler> getWriteHandlers()
	{
		return writeHandlers;
	}

	public void setWriteHandlers(List<WriteHandler> list)
	{
		writeHandlers.addAll(list);
	}

	public void addWriteHandler(WriteHandler item)
	{
		writeHandlers.add(item);
	}

	public String getSheetName()
	{
		return sheetName;
	}

	public void setSheetName(String sheetName)
	{
		this.sheetName = sheetName;
	}

	public List<WishColumn<?>> getTableHeaders()
	{
		return tableHeaders;
	}

	public void setTableHeaders(List<WishColumn<?>> tableHeaders)
	{
		this.tableHeaders = tableHeaders;
	}

	public void setDatasSupplier(Supplier<List<?>> datasSupplier)
	{
		this.datasSuppliers.add(datasSupplier);
	}

	public void setDatasSuppliers(List<Supplier<List<?>>> datasSuppliers)
	{
		this.datasSuppliers = datasSuppliers;
	}

	public List<List<String>> getHeaders()
	{
		return WishColumn.getHeaders(tableHeaders);
	}

	public List<WishColumn<?>> getLeafs()
	{
		return WishColumn.getLeafs(tableHeaders);
	}

	public Integer getNumber()
	{
		return number;
	}

	public void setNumber(Integer number)
	{
		this.number = number;
	}

	public List<List<Object>> getDatas()
	{
		List<WishColumn<?>> leafs = WishColumn.getLeafs(tableHeaders);
		List<List<Object>> allDatas = new ArrayList<>();
		for (int i = 0; i < datasSuppliers.size(); i++)
		{
			Supplier<List<?>> supplier = datasSuppliers.get(i);
			List<?> datas = supplier.get();
			final int ii = i;
			List<List<Object>> datass = LambdaUtils.list2list(datas, x -> {
				return LambdaUtils.list2list(leafs, tableHeader -> tableHeader.getFunctions().get(ii).applyObject(x));
			});
			allDatas.addAll(datass);
		}
		return allDatas;
	}
}
